import React from "react";
import {connect} from "dva";
import {Layout, Menu, Breadcrumb, Icon} from "antd";
import {Link} from "dva/router";
import {Loader} from "../components";
import NProgress from "nprogress";
import PropTypes from "prop-types";
import "antd/dist/antd.css";
import {auth,isInArray} from '../utils'

const {Header, Content, Footer, Sider} = Layout
const {SubMenu, Item} = Menu
let lastHref;

// 不需要登录就可以访问的页面
const notLogin = ['/login','/error']

console.log('是不是只运行一次');

function App({children, location: {pathname}, loading: {global}}) {

  const authLoading = false
  const rootUrl = pathname.match(/^\/[^/]*/).toString()

  console.log(isInArray(rootUrl,notLogin));

  auth.add('ssssssss')


  const href = window.location.href
  if (lastHref !== href) {
    NProgress.start()
    if (!global) {
      NProgress.done()
      lastHref = href
    }
  }

  let loading = global;

  return (
    <div>
      <Loader spinning={loading}/> {loading || <Layout>
      <Header className="header">
        <div className="logo"/>
        <Menu theme="dark" mode="horizontal"
              defaultSelectedKeys={[pathname.match(/^\/[^/]*/).toString()]}
              style={{lineHeight: '64px'}}>
          <Item key="/">
            <Link to="/">首页</Link>
          </Item>
          <Item key="/home">
            <Link to="/home">home</Link>
          </Item>
          <Item key="/setup">
            <Link to="/setup/user">setup</Link>
          </Item>
        </Menu>
      </Header>
      {children}
    </Layout>}
    </div>
  );
}

App.propTypes = {
  loading: PropTypes.object
};

export default connect(({loading}) => ({loading}))(App);
